/*
 * error.cpp
 *
 *  Created on: 07.11.2011
 *      Author: delgado
 */

#include "error.h"
#include <iostream>
using std::endl;
using std::cerr;
#include <sstream>
using std::stringstream;


#ifdef BIN_NAME
#	define strSecond(s) strFirst(s)
#	define strFirst(s) #s
#	define __BINNAME__ strSecond(BIN_NAME)
#else
#	define __BINNAME__ ""
#endif

namespace neurignacio{

void error(ErrorCode code, const int i)
{
	stringstream oss;
	oss<<i;
	error(code, oss.str());
}

void error(ErrorCode code, const string comment)
{
	string b;
	switch (code)
	{
	case NO_ARGUMENTS: // comment is help text
		cerr << __BINNAME__ <<" - ERROR - "<< NO_ARGUMENTS << ": " << comment << endl;
		throw(IS_NOT_REGULAR_FILE);
		break;
	case COMMAND_PARSER_EXCEPTION: // comment is argument
		cerr << __BINNAME__ <<" - ERROR - "<< COMMAND_PARSER_EXCEPTION << ": " << comment << endl;
		throw(COMMAND_PARSER_EXCEPTION);
		break;
	case IS_NOT_REGULAR_FILE: // comment is the FILE that is not a regular file
		cerr << __BINNAME__ <<" - ERROR - "<< IS_NOT_REGULAR_FILE << ": " << comment << " not found or is not a regular file" << endl;
		throw(IS_NOT_REGULAR_FILE);
		break;
	case FILENAME_IS_EMPTY: // comment is the FILE from which the filename is empty
		cerr << __BINNAME__ <<" - ERROR - "<< FILENAME_IS_EMPTY << ": No input file was given " << comment << endl;
		throw(FILENAME_IS_EMPTY);
		break;
	case CANNOT_OPEN_FILE: // comment is the Filename that cannot be opened
		cerr << __BINNAME__ <<" - ERROR - "<< CANNOT_OPEN_FILE << ": "<< comment <<" could not be opened" << endl;
		throw(CANNOT_OPEN_FILE);
		break;
	case CANNOT_CREATE_FILE: // comment is the Filename that cannot be created
		cerr << __BINNAME__ <<" - ERROR - "<< CANNOT_CREATE_FILE << ": "<< comment <<" could not be created" << endl;
		throw(CANNOT_CREATE_FILE);
		break;
	case FILE_FORMAT_ERROR: // comment is the Filename with unrecognized file format
		cerr << __BINNAME__ << " - ERROR - " << FILE_FORMAT_ERROR << ": " << comment <<" file format could not be recognized" << endl;
		throw(FILE_FORMAT_ERROR);
		break;
	case CHILDREN_MISSMATCH: // number of data segments in tiRoot is not equals to EXPECT_NUMBER_OF_CHILDREN
		#define EXPECT_NUMBER_OF_CHILDREN 7
		cerr << __BINNAME__ << " - ERROR - " << CHILDREN_MISSMATCH << ": " << "I found " << comment << " data segments but exactly " << EXPECT_NUMBER_OF_CHILDREN << " are expected"<< endl;
		throw(FILE_FORMAT_ERROR);
		break;
	case FILE_EXISTS: // comment is the Filename with unrecognized file format
		cerr << __BINNAME__ << " - ERROR - " << FILE_EXISTS << ": " << comment <<" file already exists. Use -f to force overwrite" << endl;
		throw(FILE_EXISTS);
		break;
	case SEGMENT_MISMATCH: // comment is the name of the segment that doesn't match
		cerr << __BINNAME__ << " - ERROR - " << SEGMENT_MISMATCH << ": " << "Segment does not match " << comment << " segment description" << endl;
		throw(FILE_FORMAT_ERROR);
		break;
	case TYPE_OF_DATA_UNKNOWN: // comment is the name of the segment that received the data and the type of data separated by "*"
	{
		size_t pos=comment.find("*");
		if (pos!=string::npos)
		{
			string segmentName=comment.substr(0,pos);
			string typeName=comment.substr(pos+1);
			cerr << __BINNAME__ << " - ERROR - " << -1*TYPE_OF_DATA_UNKNOWN << ": " << segmentName << " received an unknown Type of Data (" << typeName << ")" << endl;
		}
		else
		{
			cerr << __BINNAME__ << " - ERROR - " << -1*TYPE_OF_DATA_UNKNOWN << ": " << " Unknown Type of Data received (" << comment << ")" << endl;
		}
		throw(TYPE_OF_DATA_UNKNOWN);
		break;
	}
	case AMPLIFIER_NOT_FOUND: // comment is the number of the amplifier that wasn't found
		cerr << __BINNAME__ << " - ERROR - " << AMPLIFIER_NOT_FOUND << ": " << "There is no amplifier #" << comment << " in this file" << endl;
		throw(AMPLIFIER_NOT_FOUND);
		break;
	default:
		throw(-1);
	}
}

}
